package j.framework.base.controller;

import j.framework.core.FApplication;
import j.framework.core.LoginUser;
import j.data.tree.TreeData;
import j.util.DateUtils;
import j.util.web.BaseController;
import j.util.web.HttpView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * User: jason
 * Date: 13-9-27 上午9:54
 */
@WebServlet(urlPatterns = "/user/center")
public class UserHomeController extends BaseController {

    @Override
    protected void setActionMapper(Map<String, String> mapper) {
        mapper.put("main","doShowMainUI");
        mapper.put("logout","doLogout");
        mapper.put("menu","doGetUserMenu");

        mapper.put("minfo","doShowMemberInfoUI");//显示用户信息页面
    }

    @Override
    public void doDefaultGet(HttpView view) throws ServletException, IOException {
        view.setAttribute("siteName", FApplication.getSiteName());
        view.setAttribute("rightTopMsg", FApplication.getSetting("top_right_msg"));
        view.setAttribute("userName", view.getUser().getName());
        view.setAttribute("now", DateUtils.getCurrentFormatedDate());
        view.forward("home/index");
    }

    public void doShowMainUI(HttpView view)throws ServletException,IOException{
        view.forward("home/main");
    }

    public void doLogout(HttpView view)throws ServletException,IOException{
        HttpSession session = view.getRequest().getSession(false);
        session.setAttribute(LoginUser.SESSION_KEY, null);
        session.invalidate();
        view.redirect("/login");
    }

    public void doGetUserMenu(HttpView view)throws ServletException,IOException{
        LoginUser user = view.getUser();
        TreeData td = FApplication.getMembershipProvider().getUserFunctionTree(user);
       String treeData=td.toString();
        treeData.replaceAll(",\"text\"",",\"state\":\"closed\",\"text\"");
        view.toText(treeData);
    }
}
